import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBNavigator.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBCommonToast/YBCommonToastPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginMainPage/YBLoginProvider/YBEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginMainPage/YBLoginProvider/YBEventPassWordBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginMainPage/YBLoginProvider/YBLoginSelectEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginHeaderInfoPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginPassWordTextField.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginSubPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginYinSiPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBAPi/YBDioUtils.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBLoginInfoModule/YBLoginInfoApi.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBUtils/YBBaseUtils/YBisLoginUtils.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineAuthVipModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineUserInfoModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBBaseMineNetWork/YBBaseMineNetWork.dart';

class YBLoginPassWordPage extends StatefulWidget {
  @override
  _YBLoginPassWordPageState createState() => _YBLoginPassWordPageState();
}

class _YBLoginPassWordPageState extends State<YBLoginPassWordPage> {

  var _account;
  var _pwd;
  bool select = false;

  @override
  void initState() {
    super.initState();
    eventBus.on<YBPhoneEventBus>().listen((event) {
       setState(() {
         _account = event.phoneNumber;
       });

    });

    pwdBus.on<YBEventPassWordBus>().listen((event) {
      setState(() {
        _pwd = event.pwdStr;
      });
    });
    selectEventBus.on<YBLoginSelectEventBus>().listen((event) {
      select = event.loginSelect;
    });

  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text('密码登录'),
        ),
        body: SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.only(left: 20,right: 20,bottom: 10,top: 10),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Container(
                  child: YBLoginHeaderInfoPage(),
                ),
                Container(
                  height: 30,
                ),
                Container(
                  child: Text("密码登录",style: YBTextStyle.login1C1D1F28SemiBold,),
                ),
                Container(
                  height: 30,
                ),
                Container(
                  child: YBLoginSubPage(),
                ),
                Container(
                  child: YBLoginPassWordTextField(),
                ),
                Container(
                  height: 30,
                ),
                Container(
                  height: 44,
                  child: RawMaterialButton(
                      child: Text("登录",style:YBTextStyle.xieGangQingNianTuiGuangYuanBtnText,),
                      fillColor: YBColor.commonThemeColor,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(22.0)),
                      ),
                      onPressed:(){
                        if (select == false){
                          YBToast.showToastInfo('您没有同意协议');
                          return;
                        }
                        _initLoginIn(_pwd, _account,context);
                      }
                  ),
                ),
                Container(
                  height: 20,
                ),
                Container(
                  child: YBLoginYinSiPage(),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _initLoginIn(String msgPassWord,String account,BuildContext context){
    SpUtil.remove('token');
    SpUtil.remove("VIP");
    YBDioUtils.clear();
    EasyLoading.show(status: '登录中...');
    YBLoginInfoApi.loginInPassWord({
      "customerAccount":account,
      "customerPassword":EncryptUtil.encodeMd5(msgPassWord),
      "sysCode":"xg_youbang",
    },success: (data){
      print("----$data");
      EasyLoading.dismiss();
      SpUtil.putString("token",data);
      print("-存值是成功了--${SpUtil.putString("token",data)}");
      print("--存在数据--${SpUtil.getString("token")}");
      // YBApplication.router.navigateTo(context, '/',replace: true);
      _isPhoneNumberDisplay(context);

    },fail: (error){
      EasyLoading.dismiss();
      print("----$error");
    });
  }

  static void _isPhoneNumberDisplay(BuildContext context){
    YBMineNetWork.mineUserInfoData({
      "sysCode":"xg_newyijiwang",
    },success:(data){
      print('--个人信息---$data--');
      EasyLoading.showSuccess('登录成功',duration: Duration(seconds: 1));
      var mineUserInfoModel = YBMineUserInfoModel.fromJson(data);

      if (mineUserInfoModel.tagCodeSet != null){
        if (mineUserInfoModel.tagCodeSet.contains("cow_lawyer") || mineUserInfoModel.tagCodeSet.contains("real_cow_lawyer") ||
            mineUserInfoModel.tagCodeSet.contains("good_select") || mineUserInfoModel.tagCodeSet.contains("real_good_select")){
          SpUtil.putString("MIShuTong", "1");
        }
      }

      if (mineUserInfoModel.customerAuthorities != null) {
        for (YBMineAuthVipModel mineAuthModel in mineUserInfoModel
            .customerAuthorities) {
          if (mineAuthModel.authority.contains('BLVIP')) {
            SpUtil.putString("VIP", "1");
          }else if (mineAuthModel.authority.contains("STORE")){
            SpUtil.putString("STORE", "1");
          }else if (mineAuthModel.authority.contains("LAWYER")){
            SpUtil.putString("LAWYER", "1");
          }
        }
      }
      Future.delayed(Duration(seconds: 0),(){
        YBNavigatorUtil.goToHomeRemovePage(context);
      });
    },fail: (error){
      print('--$error-');

    });
  }


}


